<?php

if(isset($_POST['cuDirId']) && isset($_POST['toDirId']) && isset($UID)){
#if it is a dirAdd request

  $cuDirId = $_POST['cuDirId'];
  $toDirId = $_POST['toDirId'];

  require_once $BASE_PATH . 'm/dir.php';
  $dircn = new DIR(DBHOST, DBUSER, DBPASS, DBNAME);
  $cuDir = $dircn->getById($cuDirId);
  $orDir = $dircn->getById($cuDir['parent']);
  $toDir = $dircn->getById($toDirId);

  $suDir = $dircn->getAllSubDirIds($cuDir['id']);

  if($cuDir === false){
    echo '{"code": 1, "message": "dir not exist"}';
  }else if($toDir === false){
    echo '{"code": 2, "message": "dir not exist"}';
  }else if($orDir['id'] === $toDir['id']){
    echo '{"code": 3, "message": "already there"}';
  }else if($cuDir['uid'] !== $toDir['uid']){
    echo '{"code": 4, "message": "not belong to"}';
  }else if(in_array($toDir['id'], $suDir) || $cuDir['id'] === $toDir['id']){
    echo '{"code": 5, "message": "noselfcontain"}';
  }else{
    $dircn->alt(
      $cuDir['id'],
      $toDir['id'],
      $cuDir['name'],
      $cuDir['subdir'],
      $cuDir['file'],
      $cuDir['uid']
    );
    $dircn->alt(
      $orDir['id'],
      $orDir['parent'],
      $orDir['name'],
      $orDir['subdir'] - 1,
      $orDir['file'],
      $orDir['uid']
    );
    $dircn->alt(
      $toDir['id'],
      $toDir['parent'],
      $toDir['name'],
      $toDir['subdir'] + 1,
      $toDir['file'],
      $toDir['uid']
    );
    require_once $BASE_PATH . 'm/bookmark.php';
    $bmcn = new Bookmark(DBHOST, DBUSER, DBPASS, DBNAME);
    $bookmark = $bmcn->get($UID);
    echo '{"code": 0, "data":' . json_encode($bookmark) . '}';
  }
}